// add a "require" function to the global scope (global object) which is smarter
// http://en.wikipedia.org/wiki/Monkey_patch ;)
require('js/lib/require_patch').monkeypatch(this);

//add variables to the global scope to which we may choose to
//intentionally add items to
var globals = {}, models = {};

//create a private scope to prevent further polluting the global object
(function() {
	
	var AppTabGroup = require('js/ui/AppTabGroup'),
		UtilityWindow = require('js/ui/UtilityWindow'),
		SettingsView = require('js/ui/SettingsView'),
		MainView = require('js/ui/MainView'),
		TestSelectionView = require('js/ui/TestSelectionView'),
		ProviderSelectorWin = require('js/ui/ProviderSelectorWin'),
		PatientSelectorWin = require('js/ui/PatientSelectorWin'),
		ui = require('js/ui/common/ui'),
		Db = require('js/lib/Db');
		
	globals.testView = require('js/ui/TestView');		
	globals.timers = [];
	globals.db = new Db();

	var Util = require('js/ui/common/Util');
	globals.util = new Util();

	// Size views
	globals.deviceFactor = globals.util.deviceFactor();
	
	// Select the provider
	var providerId = Ti.App.Properties.getInt('providerId');
	Ti.API.debug('Previous provider id: ' + providerId);
	globals.providerWin = new ProviderSelectorWin(providerId);
	globals.patientWin = new PatientSelectorWin({selected: function(e){}});
 	
	// //create our global tab group
	var mainView = new MainView();
	globals.notesWin = new UtilityWindow('Patient Notes',  mainView, new SettingsView());
	var testSel = new TestSelectionView();
	globals.testWin = new UtilityWindow('Movement Tests', testSel, new SettingsView());
	ui.addCustomButtonView(globals.testWin,new testSel.filteredViewControl());
	globals.tabs = new AppTabGroup(
		{
			title: 'Notes',
			icon: 'images/KS_nav_ui.png',
			window: globals.notesWin
		},
		{
			title: 'Testing',
			icon: 'images/KS_nav_views.png',
			window: globals.testWin
		}
	);
	
	// Stack windows for start-up
	globals.tabs.open();
	if (Ti.Platform.osname === 'android') {
		globals.tabs.addEventListener('open',function(e){
			globals.patientWin.open();	
			globals.patientWin.addEventListener('open',function(e){
				globals.providerWin.open();			
			});
		});
	}
	else {
		globals.patientWin.open();	
		globals.providerWin.open();			
	}
			
	globals.testWin.addEventListener('open', function(e) {
		// load up assets into memory
		var activity = Ti.UI.createActivityIndicator({message: 'Loading Assets, Please wait...'});
		activity.show();
		globals.images = globals.util.images();
		activity.hide();
		activity = null;
	});
	
	Ti.App.addEventListener('app.appointment.change', function(e){
		mainView.updateAppointment(e.appointment);
	});
	
	Ti.App.addEventListener('app.patient.change', function(e){
		globals.providerWin.updatePatient(globals.patient.fullName());
		globals.patientWin.updatePatient(globals.patient.fullName());
		globals.notesWin.updatePatient(globals.patient.fullName());
		globals.testWin.updatePatient(globals.patient.fullName());
	});
	
	Ti.App.addEventListener('app.provider.change', function(e){
		globals.providerWin.updateProvider(globals.provider.firstName);
		globals.patientWin.updateProvider(globals.provider.firstName);
		globals.notesWin.updateProvider(globals.provider.firstName);
		globals.testWin.updateProvider(globals.provider.firstName);
	});
})();
